package com.wang.work;

import java.io.File;
import java.io.IOException;

public class DeleteTest {

	public static void main(String[] args) throws IOException {
		File file = new File("F:/test");
//		System.out.println(deleteAll(file));
//		System.out.println(getSize(file));
//		System.out.println(getFilesSize(file));
//		File.createTempFile("_haha", "_temp",new File("f:/temp"));
//		System.out.println(Long.MAX_VALUE);
		test(file);
	}
	public static void test(File file) throws IOException{
		if(file.isFile()) {
			if(file.getName().endsWith(".txt"))
				System.out.println(file.getName());
		}else{
			File file2 = new File(file.getPath()+"/"+file.getName()+".txt");
			file2.createNewFile();
			File[] listFiles = file.listFiles();
			for (int i = 0; i < listFiles.length; i++) {
				test(listFiles[i]);
			}
		}
	}
	public static String getSize(File file) {
		if (file == null || !file.exists()) {
			return "路径错误";
		}
		File[] lf = file.listFiles();
		long sum = 0;
		if (lf != null) {
			for (File f : lf) {
				if (f.isFile()) {
					sum += f.length();
					System.out.println(sum);
				} else if (f.isDirectory()) {
					getSize(f);
				}
			}
		} else if (file.isFile()) {
			sum += file.length();
			System.out.println(sum);
		}
		return sum + "字节";
	}
	public static boolean deleteAll(File file){
		if(file == null || !file.exists()) return false;
		if(file.isFile()){
			return file.delete();
		}else{
			File[] listFiles = file.listFiles();
			if(listFiles.length == 0) file.delete();
			for (int i = 0; i < listFiles.length; i++) {
				deleteAll(listFiles[i]);
			}
			return file.delete();
		}
	}
	public static Long getFilesSize(File file){
		if(file == null || !file.exists()) return 0L;
		if(file.isFile()) 
			return (Long)file.length();
		else{
			File[] listFiles = file.listFiles();
			if(listFiles.length == 0)return 0L;
			Long sum = 0L;
			for (int i = 0; i < listFiles.length; i++) {
				sum += getFilesSize(listFiles[i]);
			}
			return sum;
		}
	}
}
